package com.ccp.dev.system.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.system.dao.DictionaryDao;
import com.ccp.dev.system.model.Dictionary;
import com.ccp.dev.system.dao.GlobalTypeDao;
import com.ccp.dev.system.model.GlobalType;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * BPM_FORM_FIELD service类
 *
 * @author zhaowj
 */
@Service
public class DictionaryService extends BaseService<Dictionary> {

    @Resource
    private DictionaryDao dictionaryDao;

    @Resource
    private GlobalTypeDao globalTypeDao;

    /**
     * 根据分类表的nodekey获取数据字典数据。
     *
     * @param nodeKey 节点key
     * @param needRoot 是否需要根节点
     * @return 数据字典数据
     */
    public List<Dictionary> getDicByNodeKey(String nodeKey,boolean needRoot) {
        GlobalType globalType = globalTypeDao.getByDictNodeKey(nodeKey);
        if (globalType == null) {
            return new ArrayList<>();
        }
        String typeId = globalType.getTypeId();
        List<Dictionary> dictionaries = dictionaryDao.getByTypeId(typeId);
        Dictionary dictionary = new Dictionary();
        GlobalType currentGlobalType = globalTypeDao.getByDictNodeKey(nodeKey);
        if(needRoot){
            dictionary.setTypeId(currentGlobalType.getTypeId());
            dictionary.setItemName(currentGlobalType.getTypeName());
            dictionary.setParentId("0");
            dictionary.setDicId(currentGlobalType.getTypeId());
            dictionaries.add(0,dictionary);
        }
        return dictionaries;
    }

    /**
     * 根据分类表的nodekey获取数据字典数据。
     *
     * @param nodeKey 节点key
     * @return 树形结构数据字典信息
     */
    public Map<String, Object> getTreeDicByNodeKey(String nodeKey) {
        GlobalType globalType = globalTypeDao.getByDictNodeKey(nodeKey);
        if (globalType == null) {
            return new HashMap<>(2);
        }
        String typeId = globalType.getTypeId();
        int type = globalType.getType();
        List<Dictionary> dictionaries = dictionaryDao.getDicByParentId(typeId);
        Map<String, Object> dataMap = new HashMap<>(2);
        //展示形式 0平铺 1树形
        dataMap.put("type", type);
        //树形结构数据字典信息
        if (type == Dictionary.SHOWTYPE_TREELIST) {
            dataMap.put("dicList", processList(dictionaries));
        } else if (type == Dictionary.SHOWTYPE_LIST) {
            //平铺结构数据字典信息
            Map<String,String> map = new HashMap<>();
            String dicPath = globalType.getTypeId();
            map.put("nodePath",dicPath+"%");
            List<Dictionary> dictionaryList = dictionaryDao.getDicByNodePath(map);
            dataMap.put("dicList", dictionaryList);
        }
        return dataMap;
    }

    /**
     * 组装成layui需要的树形结构
     *
     * @param list 分类集合
     * @return 分类树
     */
    private List<Dictionary> processList(List<Dictionary> list) {
        return getChildList(list);
    }

    /**
     * 获取子分类集合
     *
     * @param parentId 父节点ID
     * @return 子分类集合
     */
    public List<Dictionary> getChildren(String parentId) {
        List<Dictionary> children = dictionaryDao.getDicByParentId(parentId);
        return getChildList(children);
    }

    /**
     * 获取子分类集合
     *
     * @param list 分类集合
     * @return 子分类集合
     */
    private List<Dictionary> getChildList(List<Dictionary> list) {
        List retList = new ArrayList<>();
        for (Dictionary dictionary : list) {
            Map map = new HashMap();
            map.put("title", dictionary.getItemName());
            map.put("id", dictionary.getDicId());
            map.put("children", getChildren(dictionary.getDicId()));
            map.put("spread",true);
            retList.add(map);
        }
        return retList;
    }

    /**
     * 根据分类表的nodekey获取数据字典数据。
     *
     * @param nodeKey 节点key
     * @return 数据字典数据
     */
    public List<Dictionary> getByNodeKey(String nodeKey) {
        GlobalType globalType = globalTypeDao.getByDictNodeKey(nodeKey);
        if (globalType == null) {
            return new ArrayList<>();
        }
        String typeId = globalType.getTypeId();
        return dictionaryDao.getByTypeId(typeId);
    }

    public List<Dictionary> getDicByItemKey(Dictionary dictionary) {
        return this.dictionaryDao.getDicByItemKey(dictionary);
    }

    /**
     *根据项值查询信息
     * @param dictionary
     * @return
     */
    public List<Dictionary> getDicByItemValue(Dictionary dictionary) {
        return this.dictionaryDao.getDicByItemValue(dictionary);
    }

    /**
     * 根据dicId进行删除数据信息
     * @param dicId 主键id
     * @return 返回删除成功或失败条数
     */
    public int delByDicId(String dicId){
        Dictionary dictionary = baseDao.getById(dicId);
        String dicPath = dictionary.getNodePath();
        Map<String,String> map = new HashMap<>();
        map.put("nodePath",dicPath+"%");
        List<Dictionary> dicList = dictionaryDao.getDicByNodePath(map);
        int result = 0;
        for(Dictionary dic: dicList){
            baseDao.delById(dic.getDicId());
            result+=1;
        }
        return result;
    }


}